
var helper = require("../../javascript/helper");
var db = require("../../javascript/mysql");
var mysql = new db.mysqlClient("accident");
var spider = function () {
    var that = this;
    this.genProductInfo = function () {
        let days = [];
        let projects = [];
        for (i = 1; i < 366; i++) {
            days.push(i.toString()+"d")
        }
        plan = {
            "year": ["30-80"],
            "days": days,
            "people_count": ["1"],
            "accident_death": ["100000"],
            "accident_disable": ["100000"],
            "cards_fee": ["10000"]
        }
        data = helper.product([plan["year"], plan["days"], plan["people_count"], plan["accident_death"], plan["accident_disable"], plan["cards_fee"]])
        for (i = 0; i < data.length; i++) {
            dt = helper.formatProductInfo(data[i], plan);
            projects.push(dt);

        }
        return projects;


    }
    this.calcPremium = function (dt) {

        let dateDiff = Number(dt["days"].replace("d", ""));

        if (1 <= dateDiff && dateDiff <= 7) {
            return "4.00";
        }
        else if (7 < dateDiff && dateDiff <= 10) {
            return "5.00";
        }
        else if (10 < dateDiff && dateDiff <= 15) {
            return "8.00";
        }
        else if (15 < dateDiff && dateDiff <= 20) {
            return "11.00";
        }
        else if (20 < dateDiff && dateDiff <= 30) {
            return "15.00";
        }
        else if (30 < dateDiff && dateDiff <= 45) {
            return "22.00";
        }
        else if (45 < dateDiff && dateDiff <= 60) {
            return "27.00";
        }
        else if (60 < dateDiff && dateDiff <= 75) {
            return "36.00";
        }
        else if (75 < dateDiff && dateDiff <= 90) {
            return "41.00";
        }
        else if (90 < dateDiff && dateDiff <= 180) {
            return "56.00";
        }
        else if (180 < dateDiff && dateDiff <= 365) {
            return "162.00"
        }

    }
    this.main = function () {
        let filters =[];
        data = that.genProductInfo();
        for (i = 0; i < data.length; i++) {
            dt = data[i];
            price = this.calcPremium( dt );
            if( !filters.includes( price )){
                filters.push( price );
                rs = {
                    name:"境外旅行意外险（含证件重置）",
                    company:"中国大地保险",
                    price:price
                }
                result = Object.assign( dt,rs );
                mysql.insertData( result );

                
            }
        }
        mysql.close();
    }

}
